package giit.computerbase.action;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import giit.computerbase.util.Application;
import giit.computerbase.util.DealWithCharcter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2018-11-28 Wednesday 21:11:47
 */
public class DownloadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding((String) Application.getConfig().pageConfig.get("encode"));
        response.setCharacterEncoding((String) Application.getConfig().pageConfig.get("encode"));
        String title = request.getParameter("title");
        String name = request.getParameter("name");
        if (title != null && !title.equals("") && name != null && !name.equals("")) {
            title = URLDecoder.decode(title, "utf-8");
            name = URLDecoder.decode(name, "utf-8");
            SmartUpload smartUpload = new SmartUpload();
            smartUpload.initialize(getServletConfig(), request, response);
            File file = new File(Application.PATH + File.separator + "upload" + File.separator + title + File.separator + name);
            if (file.exists()) {
                try {
                    smartUpload.downloadFile(file.getAbsolutePath());
                } catch (SmartUploadException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
